package com.scut.scutzone.vo;

import com.scut.scutzone.exception.BusinessException;
import lombok.Data;
import org.springframework.http.HttpStatus;

/**
 * @author: lifeng Chan
 * @date: 2021/4/28 15:20
 * @description:
 * 业务值对象，根据zekai给出的api定义响应体的主体格式
 * 包含信息码与信息，数据体
 */
@Data
public class Result {
    private int status;
    private String message;
    private Object data;

    /**
     * 根据业务错误对象，构造错误信息响应体
     * @param e
     * @return
     */
    public static Result error(BusinessException e){
        Result result =new Result();
        result.status=e.code();
        result.message=e.getMessage();
        result.data=null;

        return result;
    }

    public static Result build(int code, String msg, Object data){
        Result result =new Result();
        result.status=code;
        result.message=msg;
        result.data=data;
        return result;
    }

    public static Result success(String msg, Object data){
        Result r=new Result();
        r.status= HttpStatus.OK.value();
        r.message=msg;
        r.data=data;
        return  r;
    }

    public static Result success(Object data){
        Result r=new Result();
        r.message="";
        r.status=HttpStatus.OK.value();
        r.data=data;
        return r;
    }
}
